package cn.itrip.trade.service;

import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.common.EmptyUtils;
import cn.itrip.dao.hotelorder.ItripHotelOrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Override
    public ItripHotelOrder loadItripHotelOrder(String orderNo) throws Exception {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("orderNo",orderNo);
        List<ItripHotelOrder> results = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
        if (results.size()>0){
            return  results.get(0);
        }
        return null;
    }

    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) throws Exception {
        //更新订单状态、支付宝交易号
        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        itripHotelOrder.setOrderStatus(2);//支付成功
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
        //增加订单后续待处理记录

        //通知业务模块后续处理
    }

    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        itripHotelOrder.setOrderStatus(1);//支付状态：已取消
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
    }

    @Override
    public boolean processed(String orderNo) throws Exception {
        // TODO Auto-generated method stub
        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        return itripHotelOrder.getOrderStatus().equals(2)&&!EmptyUtils.isEmpty(itripHotelOrder.getTradeNo());
    }
}
